const TOKEN_KEY = 'USER_INFO'
const REPORT = 'REPORT_INFO'

// 存储数据
const setItem = (key, value) => {
    if (typeof value === 'object') {
        value = JSON.stringify(value)
    }
    window.localStorage.setItem(key, value)
}

//获取数据
const getItem = (key) => {
    const data = window.localStorage.getItem(key)
    try {
        return JSON.parse(data)
    } catch (e) {
        return data
    }
}
//删除数据
const removeItem = (key) => {
    window.localStorage.removeItem(key)
}

const state = {
    user: getItem(TOKEN_KEY),
    report: getItem(REPORT)
}

const mutations = {
    setUser(state, data) {
        state.user = data
        setItem(TOKEN_KEY, state.user)
        // window.localStorage.setItem(TOKEN_KEY, JSON.stringify(state.user))  // 未封装
    },
    setReport(state, data) {
        state.report = data
        setItem(REPORT, state.report)
    },
}


export default {
    namespaced: true,
    state,
    mutations,
}